/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package com.ares.service.util;

import java.net.Authenticator;
import java.util.Properties;
import javax.mail.Message;
import javax.mail.PasswordAuthentication;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;

/**
 *
 * @author edgar
 */
public class EnviarEmailGmail {

    public EnviarEmailGmail(){
    }
    
    public String EnviarEmailGmail(String from,
            String alias_from,
            String to,
            String asunto,
            String texto) throws Exception {
    	String correoGmail="cmorales.corporacion@gmail.com";
		try{
			Properties props = System.getProperties();
			   props.put("mail.smtp.user",correoGmail);
		        props.put("mail.smtp.host", "smtp.gmail.com");
		        props.put("mail.smtp.port","465");
		        props.put("mail.smtp.starttls.enable", "true");
		        props.put("mail.smtp.auth", "true");
		        props.put("mail.smtp.socketFactory.port", "465");
		        props.put("mail.smtp.socketFactory.class",
		                "javax.net.ssl.SSLSocketFactory");
		        props.put("mail.smtp.socketFactory.fallback", "false");

		        SecurityManager security = System.getSecurityManager();

		        autentificadorSMTP auth = new autentificadorSMTP();
		        Session session = Session.getInstance(props, auth);
			//Session session = Session.getDefaultInstance(props, null);
			MimeMessage message = new MimeMessage(session);
			message.setFrom(new InternetAddress(correoGmail));
			
			//message.setRecipient(type, address) 
			message.addRecipient(Message.RecipientType.TO,new InternetAddress(correoGmail));
			//   message.addRecipient(Message.RecipientType.,new InternetAddress(to));
			message.setSubject(asunto);
			message.setText(texto, "ISO-8859-1","html");
			Transport.send(message);
			
			System.out.println("ENVIADO A :"+to);
			System.out.println("TEXTO :"+texto);
			
			return "1";
		}catch(Exception e){
		System.out.println("ERROR AL ENVIAR CORREO "+e.getMessage());
		//e.printStackTrace();
		return "Error - No se pudo enviarCorreo : "+e.getMessage();
		}
		
		
}
    private class autentificadorSMTP extends javax.mail.Authenticator {
        public PasswordAuthentication getPasswordAuthentication() {
            return new PasswordAuthentication("cmorales.corporacion@gmail.com", "");
        }
    }
}